package com.semp.jadoma.admin.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;

import com.semp.jadoma.admin.bean.FamilyAddBean;
import com.semp.jadoma.admin.form.FamilyAddForm;
import com.semp.jadoma.admin.utils.webapp.Forward;
import com.semp.jadoma.admin.utils.webapp.Session;
import com.semp.jadoma.core.model.Dog;
import com.semp.jadoma.core.model.Family;
import com.semp.jadoma.core.model.Dog.SexType;
import com.semp.jadoma.core.service.DogService;
import com.semp.jadoma.core.service.FamilyService;

public class FamilyAddAction extends RootAction{

	@Override
	public String executeChild(ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		FamilyAddForm familyAddForm = (FamilyAddForm) form;
		
		switch (familyAddForm.getStep()) {
		case FamilyAddForm.STEP_SEARCH_FATHER:	
			return doSearchFather(request, familyAddForm);
		case FamilyAddForm.STEP_SELECT_FATHER:
			return doSelectFather(request, familyAddForm);
		case FamilyAddForm.STEP_SEARCH_MOTHER:
			return doSearchMother(request, familyAddForm);
		case FamilyAddForm.STEP_SELECT_MOTHER:
			return doSelectMother(request, familyAddForm);
		case FamilyAddForm.STEP_SEARCH_CHILDS:
			return doSearchChilds(request, familyAddForm);
		case FamilyAddForm.STEP_SELECT_CHILDS:
			return doSelectChilds(request, familyAddForm);
		case FamilyAddForm.STEP_FAMILY_FIELDS:
			return doSetFamily(request, familyAddForm);
		default:
			break;
		}
		
		return null;
	}

	/**
	 * 
	 * @param request
	 * @param familyAddForm
	 * @return
	 */
	private String doSearchFather(HttpServletRequest request, FamilyAddForm familyAddForm) {
		String searchedName = familyAddForm.getSearchName();
		List dogList = DogService.getInstance().findDogLikeNameAndSex(searchedName, SexType.MALE);
		request.setAttribute(Session.LIST_BEAN_DOG, dogList);
		return "displayFather";
	}
	
	/**
	 * 
	 * @param request
	 * @param familyAddForm
	 * @return
	 */
	private String doSearchMother(HttpServletRequest request, FamilyAddForm familyAddForm) {
		String searchedName = familyAddForm.getSearchName();
		List dogList = DogService.getInstance().findDogLikeNameAndSex(searchedName, SexType.FEMALE);
		request.setAttribute(Session.LIST_BEAN_DOG, dogList);
		return "displayMother";
	}
	
	/**
	 * 
	 * @param request
	 * @param familyAddForm
	 * @return
	 */
	private String doSearchChilds(HttpServletRequest request, FamilyAddForm familyAddForm) {
		String searchedName = familyAddForm.getSearchName();
		List dogList = DogService.getInstance().findDogLikeNameWithoutFamily(searchedName);
		request.setAttribute(Session.LIST_BEAN_DOG, dogList);
		return "displayChilds";
	}
	
	/**
	 * 
	 * @param request
	 * @param familyAddForm
	 * @return
	 */
	private String doSelectFather(HttpServletRequest request, FamilyAddForm familyAddForm) {
		Dog father = DogService.getInstance().findDog(familyAddForm.getFatherId());
		FamilyAddBean containerBean = new FamilyAddBean();
		containerBean.setFather(father);
		request.getSession().setAttribute(Session.BEAN_FAMILY_ADD, containerBean);
		return "searchMother";
	}
	
	/**
	 * 
	 * @param request
	 * @param familyAddForm
	 * @return
	 */
	private String doSelectMother(HttpServletRequest request, FamilyAddForm familyAddForm) {
		Dog mother = DogService.getInstance().findDog(familyAddForm.getFatherId());
		FamilyAddBean containerBean = (FamilyAddBean) request.getSession().getAttribute(Session.BEAN_FAMILY_ADD);
		containerBean.setMother(mother);
		request.getSession().setAttribute(Session.BEAN_FAMILY_ADD, containerBean);
		return "searchChilds";
	}
	
	/**
	 * 
	 * @param request
	 * @param familyAddForm
	 * @return
	 */
	private String doSelectChilds(HttpServletRequest request, FamilyAddForm familyAddForm) {
		FamilyAddBean containerBean = (FamilyAddBean) request.getSession().getAttribute(Session.BEAN_FAMILY_ADD);
		long[] childIds = familyAddForm.getChildIds();
		ArrayList<Dog> childs = new ArrayList<Dog>();
		for (int i = 0; i < childIds.length; i++) {
			Dog child = DogService.getInstance().findDog(childIds[i]);
			childs.add(child);
		}
		containerBean.setChilds(childs);
		request.getSession().setAttribute(Session.BEAN_FAMILY_ADD, containerBean);
		return "completeFamily";
	}
	
	/**
	 * 
	 * @param request
	 * @param familyAddForm
	 * @return
	 */
	private String doSetFamily(HttpServletRequest request, FamilyAddForm familyAddForm) {
		FamilyAddBean containerBean = (FamilyAddBean) request.getSession().getAttribute(Session.BEAN_FAMILY_ADD);
		Family createdFamily = FamilyService.getInstance().createFamily(new Date(), containerBean.getFather().getId(), containerBean.getMother().getId());
		Iterator<Dog> childIterator = containerBean.getChilds().iterator();
		while (childIterator.hasNext()) {
			DogService.getInstance().addDogToFamily(childIterator.next().getId(), createdFamily.getId());
		}

		//Full reload
		createdFamily = FamilyService.getInstance().getFamily(createdFamily.getId());
		request.getSession().removeAttribute(Session.BEAN_FAMILY_ADD);
		request.setAttribute(Session.BEAN_FAMILY, createdFamily);
		
		return Forward.SUCCESS;
	}
	
}
